package com.smartcruise.task;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.util.TimeUtils;

import java.util.Comparator;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.OrderedRealmCollection;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 * 任务adapter
 */

public class TaskAdapter extends CustomRealmAdapter<CruiseData> {

    private Context context;

    public TaskAdapter(@NonNull Context context, @NonNull OrderedRealmCollection<CruiseData> objects) {
        super(objects);
        this.context = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main_task, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final CruiseData cruiseData = getItem(position);
        viewHolder.taskName.setText(cruiseData.getName());
//        viewHolder.taskType.setText(cruiseData.getType());
        viewHolder.taskType.setText("规定开始时间:" + TimeUtils.getSimpleTimeString(cruiseData.getShouldRunDate()));

        if (cruiseData.isFromNet()) {//从服务器拉取的数据
            viewHolder.time.setText("完成时间" + TimeUtils.getSimpleTimeString(cruiseData.getActualStopDate()));
            if (CruiseContract.AUDITED == cruiseData.getAuditStatus()) {//审核完成的任务
                if (cruiseData.isAuditSuccess()) {
                    viewHolder.taskStatus.setText(cruiseData.getAuditResult());
                } else {
                    if (cruiseData.isDataChanged()) {
                        switch (cruiseData.getUploadStatus()) {
                            case 0:
                                viewHolder.taskStatus.setText("被驳回");
                                break;
                            case 1:
                                viewHolder.taskStatus.setText("(被驳回)已上传概要数据");
                                break;
                            case 2:
                                viewHolder.taskStatus.setText("(被驳回)已上传");
                                break;
                        }
                    } else {
                        viewHolder.taskStatus.setText("被驳回");
                    }
                }
            } else {
                viewHolder.taskStatus.setText("已上传");
            }
        } else {//本地任务
            viewHolder.time.setText("预耗时" + cruiseData.getCostTime() + "小时");
            if (!cruiseData.isCopyData()) {
                viewHolder.taskStatus.setText("");
            } else {
                if (CruiseContract.TASK_IS_END.equals(cruiseData.getStatusInApp())) {//结束的任务显示上传状态
                    switch (cruiseData.getUploadStatus()) {
                        case 0:
                            viewHolder.taskStatus.setText("待上传");
                            break;
                        case 1:
                            viewHolder.taskStatus.setText("已上传概要数据");
                            break;
                        case 2:
                            viewHolder.taskStatus.setText("已上传");
                            break;
                    }
                } else {
                    viewHolder.taskStatus.setText(cruiseData.getStatusInApp());
                }
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaskWithMapActivity.start(context, cruiseData.getId());
            }
        });
        return convertView;
    }

    @Nullable
    @Override
    public CruiseData getItem(int position) {
        return super.getItem(position);
    }

    @Override
    Comparator<CruiseData> getComparator() {
        return new Comparator<CruiseData>() {
            @Override
            public int compare(CruiseData o1, CruiseData o2) {
                if (getNumber(o1) == 2 && getNumber(o2) == 2) {//已完成的任务按时间排序
                    return Long.compare(o1.getActualStopDate(), o2.getActualStopDate());
                }
                return getNumber(o1) - getNumber(o2);
            }
        };
    }

    private int getNumber(CruiseData cruiseData) {
        int number = 0;
        if (cruiseData.isCopyData()) {
            switch (cruiseData.getStatusInApp()) {
                case CruiseContract.TASK_IS_RUNNING:
                    number = 0;
                    break;
                case CruiseContract.TASK_IS_PAUSE:
                    number = 1;
                    break;
                case CruiseContract.TASK_IS_END:
                    number = 2;
                    break;
                default:
                    number = 3;
                    break;
            }
        } else {
            if (CruiseContract.AUDITED == cruiseData.getAuditStatus() &&
                    CruiseContract.AUDIT_FAIL.equals(cruiseData.getAuditResult())) {
                number = 4;
            } else {
                number = 5;
            }
        }
        return number;
    }

    static class ViewHolder {
        @BindView(R.id.taskName)
        TextView taskName;
        @BindView(R.id.taskType)
        TextView taskType;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.taskStatus)
        TextView taskStatus;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
